package com.vcs.dao;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget;
import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget;

import com.vcs.domain.Job;
import com.vcs.domain.JobApplication;

public class JobDAOImpl implements JobDAO {

	Log log = LogFactory.getLog(JobDAOImpl.class);

	@SessionTarget
	Session session;

	@TransactionTarget
	Transaction transaction;

	@Override
	public void save(Job job) {
		log.info("save(Job job)...");
		try {
			session.save(job);
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}

	}

	@Override
	public void update(Job job) {
		log.info("update(Job job)...");
		try {
			session.update(job);
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}

	}

	@Override
	public void delete(Long jobId) {
		log.info("delete(Long jobId)...");
		// String hql = "delete from Job job where jobId =" + jobId;
		String hql = "delete from Job where jobId=:jobId";
		Query query = session.createQuery(hql);
		query.setLong("jobId", jobId);
		int rowCount = query.executeUpdate();
		log.info(rowCount + " Job deleted  with jobId [" + jobId + "]");

		/*
		 * try { Job job = (Job) session.get(Job.class, jobId);
		 * session.delete(job); } catch (Exception e) { transaction.rollback();
		 * e.printStackTrace(); }
		 */
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Job> listJobsByClientId(Long clientId) {
		log.info("listJobsByClientId()...");
		List<Job> clientJobs = null;
		try {
			// clientJobs = (List<Job>)
			// session.createCriteria(Job.class).add(Restrictions.like("clientId",clientId));
			clientJobs = session.createCriteria(Job.class)
					.add(Restrictions.eq("clientId", clientId)).list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return clientJobs;
	}

	@Override
	public Job getJobById(Long jobId) {
		log.info("getJobById()...");
		Job job = null;
		try {
			job = (Job) session.get(Job.class, jobId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		log.info("Job : " + job);
		return job;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Job> listJobs() {
		log.info("listJobs()...");
		List<Job> jobs = null;
		try {
			jobs = session.createQuery("from Job").list();
			log.info("Number of Jobs fetched : " + jobs.size());
			log.info("jobs : " + jobs);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jobs;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Long getJobOwner(Long jobId) {
		Query query = session
				.createQuery("select j.clientId from Job j where j.jobId=?");
		query.setLong(0, jobId);
		List<Long> lst = query.list();
		for (Long l : lst) {
			log.info("---> " + l);
		}
		return lst.get(0);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<JobApplication> getAllJobApplications() {
		List<JobApplication> jobApplicationsList = session.createQuery(
				"from JobApplication").list();
		return jobApplicationsList;
	}

	/**
	 * gets job titles based on job ids
	 */

	@Override
	public Map<Long, String> getJobTitle(Long[] jobIds) {
		// TODO this method needs to be implemented fully.
		log.info("getJobTitle()...");
		String ss = "(";
		for (Long l : jobIds) {
			ss = ss + l.toString() + ",";
		}
		ss = ss.substring(0, ss.lastIndexOf(","));

		ss = ss + ")";
		log.info(ss);
		// String hql = "select j.jobId, j.jobTitle from Job j";
		String hql = "select j.jobTitle from Job j where j.jobId in " + ss;

		List lst = session.createQuery(hql).list();
		log.info("==> " + lst);
		for (Object s : lst) {
			log.info(s.toString());
		}

		return null;
	}

	/**
	 * This method takes an array of Job ids and returns all jobs with those ids
	 */

	@SuppressWarnings("unchecked")
	public List<Job> getJobListById(List<Long> jobIdList) {
		log.info("getJobListById()...");
				
		log.info(jobIdList.size());
		Criteria crit = session.createCriteria(Job.class).add(Restrictions.in("jobId", jobIdList));
		//Criteria crit = session.createCriteria(Job.class);
		List<Job> jobList = crit.list();
		log.info(jobList);
		for (Job j : jobList) {
			log.info("-------------> " + j);
		}
		return jobList;
	}
	
	
	
}
